<?php
	/** sous fonctionnalité de gestion des joueurs d'un matche
	 * @author Sebastian Mosser
	 */

class sfManageGoals extends aSubFeature {


	public function __construct() {
		  $this->_acl = new AccessControlList(); 
      $this->_acl->setGranted(1, AccessControlList::$JUST_ME); // Entraineur
      $this->_acl->setGranted(3); // Secretariat
      $this->_acl->setGranted(7); // Administrateur système
		}


	public function check($teamId) {
	
		$perm = $this->_acl->isExposable(Login::getGroups());
		$aTeam = Team::read($teamId);
		
		if ($perm == AccessControlList::$JUST_ME) {
			if (! $aTeam->canEdit(Login::getLoginId())) {
				$page = <<<EOS
<h1> Une erreur est survenue ! </h1>
<div align="center">
<p> <span class="error">Vous n'êtes pas autorisé a faire des modifications dans cette équipe car vous n'en êtes pas l'administrateur.</span></p>
</div>
<p> Si vous pensez qu'il s'agit d'une erreur, contactez le secrétariat du club qui pourra vous autoriser à y acceder.</p>
<p> Pour pouvoir modifier une équipe, vous devez être déclaré : 
<ul>
  <li> Administrateur de l'équipe </li>
  <li> Administrateur de catégorie </li>
  <li> Membre du groupe <code> Secretariat </li>
  <li> Membre du groupe <code> Administrateur Système </code> </li>
</ul>
EOS;
				return $page;
			}
		}
		return True;
	}

	protected function run() {
		
		$id = PropRetriever::getProp($_POST,"id");
		if ($id == "")
			$id = PropRetriever::getProp($_GET,"id");
		
		try {
			$aMatche = Matche::read($id);
			$teamId = $aMatche->getTeamId();
		}
		catch(Exception $e) {
			$ans  = "<h1> Une erreur est survenue ! </h1>";
			$ans .= "<div align=\"center\"> <span class=\"error\">";
			$ans .= "Le match demandé n'existe pas !";
			$ans .= "</span></div>";
			return $ans;
		}
		
		$c = $this->check($teamId);
		if ($c === True)
			return $this->process($aMatche,$id);
		else
			return $c;
	}
	
	// $aMatche est une instance de Matche, mais pas une rèf.
	private function process($aMatche,$id) {
		$action = PropRetriever::getProp($_POST,"action");
		if ($action == "add")
			return $this->addGoalInMatche($aMatche, $id);
		else if ($action == "delete")
			return $this->deleteGoalInMatche($aMatche,$id);
		else
			return $this->displayform($aMatche,$id);
	}
	

	private function deleteGoalInMatche($aMatche, $id) {
		$goalId = PropRetriever::getProp($_POST,"goalId");
		$aMatche->delGoal($goalId);
		$aMatche->update();
		return $this->displayForm($aMatche,$id,
															"Supression effectuée avec succès !");
	}
	
	// $aMatche est une instance de matche 
	private function addGoalInMatche($aMatche, $id) {
		$playerId     = PropRetriever::getProp($_POST,"player");
		$helperId     = PropRetriever::getProp($_POST,"helper");
		$type         = PropRetriever::getInt($_POST,"type");
		$forUs        = PropRetriever::getInt($_POST,"forUs");
		$localisation = PropRetriever::getInt($_POST,"localisation");
		$minute       = PropRetriever::getInt($_POST,"minute");
		$comment      = PropRetriever::getProp($_POST,"comment");
		
		$aMatche->addGoal($playerId,$helperId,$type, $minute, $comment,
											$localisation,$forUs);
		$aMatche->update();
		$aMatche = Matche::read($id);
		return $this->displayForm($aMatche, $id, "Ajout effectué avec succès");
	}
	
	private function displayForm($aMatche,$id,$text = "") {
		$matcheResume = $aMatche->displayShort();
				
		$playerOptions = $aMatche->getPlayersAsList(True);
		$helperOptions = $aMatche->getPlayersAsList(True);
		$forUsOptions  = Matche::getForUsAsList(Matche::$FOR_US);
		$typeOptions   = Matche::getGoalTypeAsList();
		$locOptions    = Matche::getLocalisationAsList();

		$goalsForm = "";
		$goalsContent = "";
		$goals = $aMatche->getGoals();
		$goalsArray = array();
		foreach ($goals as $k => $v) {
			$minute = $v["minute"];
			if ($v["forUs"] == Matche::$FOR_US)
				$forUs = "But Mis";
			else
				$forUs = "<span class=\"error\"> But Encaissé </span>";
				
			$player = $v["player"];
			if ($player == -1)
				$player = "<span class=\"error\" >Inconnu </span>";
			else {
				$aMember = Member::read($player);
				$player = $aMember->displayName();
			}
			$helper = $v["helper"];
			if ($helper == -1)
				$helper = "<span class=\"error\" >Inconnu </span>";
			else {
				$aMember = Member::read($helper);
				$helper = $aMember->displayName();
			}
			$type = Matche::$GOAL_TYPE[$v["type"]];
			$loc  = Matche::$GOAL_LOC[$v["localisation"]];
			$comment = nl2br(PropRetriever::getProp($v,"comment"));
			if ($comment == "")
				$comment = "--";
			$goalId = $k;
			
			$table = <<<EOS
	<tr>
		<th> ${minute} ' </th> 
		<th colspan="2" align="center"> ${forUs} </th>
		<th> 
			<form method="post">
				<input name="action" type="hidden" value="delete" />
				<input name="goalId" type="hidden" value="${goalId}" />
				<input name="id" type="hidden" value="${id}" />
				<input Type="submit" value="Supprimer ce But" class="edit"/>
			</form>
		</th>
	</tr>
	<tr>
		<td> <strong> Buteur </strong> </td> <td> ${player} </td>
		<td> <strong> Passeur </strong> </td> <td> ${helper} </td>
	</tr>
	<tr> 
		<td> <strong> Type </strong> </td> <td> ${type} </td>
		<td> <strong> Localisation </strong> </td> <td> ${loc} </td>
	</tr>
	<tr>
		<td colspan="4"> ${comment} </td>
	</tr>
EOS;
			$goalsArray[$table] = $minute;
		}
		
		asort($goalsArray);

		foreach($goalsArray as $k => $v)
			$goalsContent .= $k;
		
		if ($goalsContent != "") {
			$goalsForm = <<<EOS
<div align="center">
<table class="resultset">
$goalsContent
</table>
</div>
EOS;
		}
		else {
			$goalsForm = "<div align=\"center\"> <span class=\"error\">";
			$goalsForm .= "Aucun but rentré pour ce Match !";
			$goalsForm .= "</span> </div>";
		}
		$resultAction = "";
		if ($text != "") {
			$resultAction = "<h2> Résultat de l'action demandée </h2>";
			$resultAction .= "<div align=\"center\"><span class=\"error\">";
			$resultAction .= $text;
			$resultAction .= "</span></div>";
		}
		
		$returnButton = sfManageAMatche::getReturnButton($id);
		
		$page = <<<EOS
${matcheResume}
<br />
${returnButton}
<h1> Ajouter un But </h1>
<div align="center">
<form method="post" accept-charset="utf-8">
<table>
<tr>
<td>
  <fieldset>
		<legend> Infos Techniques </legend>
		
		<br />
		<label> 
			<span>Direction : </span>
			<select name="forUs" class="edit">
			${forUsOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Buteur : </span>
			<select name="player" class="edit">
			${playerOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Passeur : </span>
			<select name="helper" class="edit">
			${playerOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Type : </span>
			<select name="type" class="edit">
			${typeOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Localisation : </span>
			<select name="localisation" class="edit">
			${locOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		<label> 
			<span>Minute de Jeu : </span>
			<input type="text" name="minute" class="edit" />
			&nbsp; &nbsp;
		</label>
		<br />
	</fieldset>
</td>

<td> &nbsp; &nbsp; </td>

<td>
<fieldset>
<legend> Commentaire Personnel </legend>
<label>
<div align="left">
Entrez un commentaire sur le but : 
</div>
<textarea name="comment" class="edit" rows="12"> 
</textarea>

</label>
</fieldset>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<br />
		<input type="hidden" name="id" value="$id" />
		<input type="hidden" name="action" value="add" />
		<input type="submit" value="Ajouter le But" class="edit" />
</td>
</tr>
</table>
</form>
</div>

${resultAction}

<h1> Buts Renseignés </h1>
${goalsForm}

EOS;
		return $page;
	
	}
	
	
}

?>